package com.youhome.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.mapper.BClientTagMapper;
import com.youhome.service.mapper.BTagMapper;
import com.youhome.service.model.*;
import com.youhome.service.service.BCounselorService;
import com.youhome.service.service.BTagService;
import com.youhome.service.service.BUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2017/10/25.
 */
@Service
public class BTagServiceImpl implements BTagService {
    @Autowired
    private BTagMapper bTagMapper;
    @Autowired
    private BClientTagMapper bClientTagMapper;
    @Autowired
    private BCounselorService bCounselorService;

    @Override
    public int create(Long counselorId, String tag) throws Exception {
        BTag bTag = new BTag();
        bTag.setCounselorId(counselorId);
        bTag.setTag(tag);
        bTag.setStatus(1);
        bTag.setCtime(new Date());
        return bTagMapper.insertSelective(bTag);
    }

    @Override
    public int update(Long tagId, String tag) throws Exception {
        BTag bTag = new BTag();
        bTag.setTagId(tagId);
        bTag.setTag(tag);
        bTag.setUtime(new Date());
        return bTagMapper.updateByPrimaryKeySelective(bTag);
    }

    @Override
    public PageInfo<BTag> selectByBean(Integer page, BTag bTag) throws Exception {
        if (page == null || page < 1)
            page = 1;
        PageHelper.startPage(page, 10);
        List<BTag> list = bTagMapper.selectByBean(bTag);
        return new PageInfo<>(list);
    }

    @Override
    public int selectIsExist(Long CounselorId, String tag) {
        BTag bTag = new BTag();
        bTag.setCounselorId(CounselorId);
        bTag.setTag(tag);
        return bTagMapper.selectIsExist(bTag);
    }

    @Override
    public int selectCount(Long counselorId) {
        return bTagMapper.selectCount(counselorId);
    }

    @Override
    public BTag selectByPrimaryKey(Long tagId) throws Exception {
        return bTagMapper.selectByPrimaryKey(tagId);
    }

    @Override
    public int make(BClientTag clientTag) throws Exception {
        clientTag.setCtime(new Date());
        return bClientTagMapper.insertSelective(clientTag);
    }

    @Override
    @Transactional
    public int make(Long clientId, String tagIds) throws Exception {
        int success = 0;
        BClientTag clientTag = new BClientTag();
        clientTag.setClientId(clientId);

        //先删除客户原有标签
        if (bClientTagMapper.selectIsExist(clientTag) > 0) {
            bClientTagMapper.deleteByClientId(clientId);
        }
        if (tagIds.indexOf(",") > -1) {
            String[] arr_tagIds = tagIds.split(",");

            int count = 0;
            for (String id : arr_tagIds) {
                Long tagId = Long.valueOf(id);
                clientTag.setTagId(tagId);
                clientTag.setCtime(new Date());

                if (bClientTagMapper.insertSelective(clientTag) == 1) {
                    count++;
                }
            }

            if (arr_tagIds.length == count) {
                success = 1;
            }
        }

        return success;
    }

    @Override
    public Boolean isExistMake(BClientTag clientTag) throws Exception {
        if (bClientTagMapper.selectIsExistByBean(clientTag) != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional
    public int delete(Long tagId) throws Exception {
        bClientTagMapper.deleteByTagId(tagId); //删除本标签相关调用
        return bTagMapper.deleteByPrimaryKey(tagId);
    }

    @Override
    public int deleteByBean(BClientTag record) {
        return bClientTagMapper.deleteByBean(record);
    }

    @Override
    public List<BTag> selectAll(BTag bTag) {
        return bTagMapper.selectByBean(bTag);
    }

    @Override
    public List<BTag> selectByClientId(Long clientId) {
        return bTagMapper.selectByClientId(clientId);
    }


    /**
     * 查询所有顾问标签
     *
     * @param clientId 客户ID
     * @return
     * @throws CustomException
     */
    @Override
    public List<BTag> selectAllTag(Long clientId) throws CustomException {
        if (StringUtils.isEmpty(clientId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        return bTagMapper.selectByClientId(clientId);
    }

    @Override
    public void deleteByTagId(Long tagId) throws CustomException {
        if (StringUtils.isEmpty(tagId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        if (bTagMapper.deleteByPrimaryKey(tagId) == 0) {
            throw new CustomException(ResultCode.SUCCESS);
        }
    }

    @Override
    public void makeTag(Long clientId, Long tagId) throws CustomException {
        if (StringUtils.isEmpty(clientId) || StringUtils.isEmpty(tagId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        BClientTag clientTag = new BClientTag();
        clientTag.setClientId(clientId);
        clientTag.setTagId(tagId);

        //如果客户已经有此标签，则删除，没有没新增
        if (bClientTagMapper.selectIsExistByBean(clientTag) != null) {
            if (bClientTagMapper.deleteByBean(clientTag) == 0) {
                throw new CustomException(ResultCode.FAIL);
            }
        } else {
            clientTag.setCtime(new Date());
            if (bClientTagMapper.insertSelective(clientTag) == 0) {
                throw new CustomException(ResultCode.FAIL);
            }
        }

    }


    /**
     * 查询顾问所有标签
     *
     * @return
     * @throws CustomException
     */
    @Override
    public List<BTag> selectAllTag(Principal principal) throws CustomException {

        Long userId = principal.getUser().getUserId();
        BCounselor counselor = bCounselorService.selectByUserId(userId);
        if (counselor == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }
        BTag bTag = new BTag();
        bTag.setCounselorId(counselor.getCounselorId());
        bTag.setStatus(1);
        return bTagMapper.selectByBean(bTag);
    }


    /***
     * 创建标签
     * @param tag 标签
     * @throws CustomException
     */
    @Override
    public BTag createTag(Principal principal,String tag) throws CustomException {
        if (StringUtils.isEmpty(tag)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }

        Long userId = principal.getUser().getUserId();

        BCounselor counselor = bCounselorService.selectByUserId(userId);
        if (counselor == null) {
            throw new CustomException(ResultCode.DATA_DOES_NOT_EXIST_OR_HAS_BEEN_DELETED);
        }

        BTag bTag = new BTag();
        bTag.setCounselorId(counselor.getCounselorId());
        bTag.setTag(tag);

        if (bTagMapper.selectIsExist(bTag) > 0) {
            throw new CustomException(ResultCode.IS_EXIST_TAGS);
        }

        int count = bTagMapper.selectCount(counselor.getCounselorId());
        if (count >= 10) {
            throw new CustomException(ResultCode.TAGS_COUNT_EXCEED_CAPACITY);
        }
        bTag.setStatus(1);
        bTag.setCtime(new Date());
        if (bTagMapper.insertSelective(bTag) == 1) {
            return bTag;
        } else {
            throw new CustomException(ResultCode.FAIL);
        }
    }
}
